package cn.smxy.category.service.impl;

import cn.smxy.category.mapper.CategoryMapper;
import cn.smxy.category.service.CategoryService;
import cn.smxy.client.PlantClient;
import cn.smxy.param.PageParam;
import cn.smxy.pojo.Category;
import cn.smxy.util.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private PlantClient plantClient;


    /**
     * 修改种群
     *
     * @param category
     * @return
     */
    @Override
    @Caching(
            evict = {
                    @CacheEvict(value = "list.category", allEntries = true),
                    @CacheEvict(value = "category", allEntries = true)
            }
    )
    public R update(Category category) {
        int rows = categoryMapper.updateById(category);
        if (rows == 0) {
            log.info("CategoryServiceImpl.update业务结束，结果为:{}", "修改失败");
            return R.fail("修改失败");
        }
        log.info("CategoryServiceImpl.update业务结束，结果为:{}", "修改成功");
        return R.ok("修改成功");
    }

    /**
     * 获取类群信息
     *
     * @param categoryId
     * @return
     */
    @Override
    public Category getInfo(Long categoryId) {
        return categoryMapper.selectById(categoryId);
    }

    /**
     * 删除种群
     *
     * @param categoryId
     * @return
     */
    @Override
    @Caching(
            evict = {
                    @CacheEvict(value = "list.category", allEntries = true),
                    @CacheEvict(value = "category", key = "#categoryId")
            }
    )
    public R remove(Long categoryId) {
        int rows = categoryMapper.deleteById(categoryId);
        if (rows == 0) {
            log.info("CategoryServiceImpl.remove业务结束，结果为:{}", "删除失败");
            return R.fail("删除失败");
        }
        log.info("CategoryServiceImpl.remove业务结束，结果为:{}", "删除成功");
        return R.ok("删除成功");
    }

    /**
     * 分页查看种群列表
     *
     * @param pageParam
     * @return
     */
    @Override
    @Cacheable(value = "list.category", key = "#pageParam.currentPage+" +
            "'-'+#pageParam.pageSize")
    public R page(PageParam pageParam) {
        IPage<Category> page = new Page<>(pageParam.getCurrentPage(), pageParam.getPageSize());
        page = categoryMapper.selectPage(page, null);
        long total = page.getTotal();
        List<Category> records = page.getRecords();
        R ok = R.ok("查询成功", records, total);
        log.info("CategoryServiceImpl.page业务结束，结果为:{}", ok);
        return ok;
    }

    /**
     * 添加种群
     *
     * @param category 种群实体类
     * @return
     */
    @Override
    @CacheEvict(value = "list.category", allEntries = true)
    public R add(Category category) {
        int rows = categoryMapper.insert(category);
        if (rows == 0) {
            log.info("CategoryServiceImpl.add业务结束，结果为:{}", "新种群添加失败");
            return R.fail("添加失败");
        }
        log.info("CategoryServiceImpl.add业务结束，结果为:{}", "添加成功");
        return R.ok("添加成功");
    }

    /**
     * 查看种群列表
     *
     * @return
     */
    @Override
    @Cacheable(value = "list.category", key = "#root.methodName")
    public List<Category> list() {
        List<Category> categoryList = categoryMapper.selectList(null);
        log.info("CategoryServiceImpl.list业务结束，结果为:{}", categoryList);
        return categoryList;
    }
}
